home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / dev / c / MEMLib.lha / MEMLib / Developer / source.org / example.c next >
Encoding:
C/C++ Source or Header  |  1999-06-30  |  1.3 KB  |  52 lines

  1. #include <exec/memory.h>
  2. #include <proto/exec.h>
  3.  
  4. #define MWDEBUG=1
  5.  
  6. #include "memwatch.h" /* To enable memlib, you must #define MWDEBUG to 1 */
  7.  
  8. int main(void)
  9. {
  10.    char *a;
  11.    void *b;
  12.    char n[]="This is a sample text";
  13.  
  14.    a = AllocMem(20, 0);  /* Note that we never free this memory */
  15.  
  16.    a = AllocMem(10, 0);
  17.  
  18.    FreeMem(a, 9);  /* Note that we free an incorrect length here */
  19.  
  20.    a = AllocVec(10, 0);
  21.  
  22.    free(a);  /* Freed with wrong free routine */
  23.  
  24.    a = strdup("test"); /* Note that we never free this memory */
  25.  
  26.    a = getcwd(NULL, 1000);
  27.    free(a);
  28.    free(a);  /* Note that we're freeing the memory twice! */
  29.  
  30.  
  31.    /* now use standard malloc and free functions */
  32.    b = (void *)malloc(10);
  33.    free(b);
  34.  
  35.    /* now, how about assigning in a an if-statement */
  36.    if ((b = (void *)malloc(strlen(n)+1)) == NULL)
  37.       b = (void *)0xdeadbeef;
  38.  
  39.    free(b);
  40.  
  41.    putenv("MWTest=xx");
  42.    a = getenv("MWTest");
  43.    if(a) a[strlen(a)+1] = 0;  /* Note we're trashing a byte!!!      */
  44.                               /* This shouldn't cause a real crash  */
  45.                               /* since malloc() allocates in clumps */
  46.                               /* of 4 bytes, and 'a' is 3 bytes.    */
  47.  
  48.    MWReport("At end of main()", MWR_FULL);  /* Generate a memory usage report */
  49.  
  50.    return(0);
  51. }
  52.